package com.example.leetcode.shopping;

import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TaoBao {


    public static void main(String[] args) throws Exception {
        //准备驱动
        System.setProperty("webdriver.chrome.driver", "D:\\test2\\chromedriver.exe");


        //1、打开浏览器
        ChromeDriver browser = new ChromeDriver();
        Actions actions = new Actions(browser);

        //2、输入网址
        browser.get("https://www.taobao.com");
        Thread.sleep(3000);

        //3、点击登录
        browser.findElement(By.linkText("亲，请登录")).click();
        Thread.sleep(2000);

        //4、扫码登录，线程休息5秒
        browser.findElement(By.className("icon-qrcode")).click();//扫码登录
        Thread.sleep(6000);

        //5、进入购物车页面
        // https://cart.taobao.com/cart.htm
        browser.get("https://cart.taobao.com/cart.htm");
        Thread.sleep(5000);

        //6、点击选择第一个按钮
        // //*[@id="J_CartMain"]/div[1]/div/div[3]/div
        ////*[@id="J_Order_s_748771569_1"]
        // //*[@id="J_Order_s_748771569_1"]
        //*[@id="J_Order_s_2214735940849_1"]
        // J_Order_s_2214735940849_1
        // //*[@id="J_OrderHolder_s_748771569_1"]
        // //*[@id="J_Order_s_748771569_1"]
        //*[@id="J_Order_s_748771569_1"]


        // //*[@id="J_Order_s_2928278102_1"]
        //*[@id="J_Order_s_2599134047_1"]
        // //*[@id="J_SelectAll1"]
        //*[@id="J_Order_s_2214069938125_1"]
        //   browser.findElement(By.xpath("//*[@id=\"J_Order_s_748771569_1\"]/div[1]/div/div/label")).click();
        // //*[@id="J_Order_s_725677994_1"]
        //  browser.findElement(By.xpath("//*[@id=\"J_Order_s_725677994_1\"]/div[1]/div/div/label")).click();
        // //*[@id="J_Order_s_2214735940849_1"]
        // //*[@id="J_Order_s_2214735940849_1"]
        //*[@id="J_Order_s_2599134047_1"]
        //  Thread.sleep(2000);

        //7、循环检查结算按钮，如果倒计时结束，迅速点击
        //0、设置秒杀时间
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse("2022-12-25 12:00:00");
        Date endTime = sdf.parse("2022-12-25 12:01:00");
        while (true) {
            //当前时间
            Date now = new Date();
            if (now.after(endTime)) {
                System.out.println("超过了时间");
                break;
            }
            if (now.after(date) || now.equals(date)) {
                if (browser.findElement(By.linkText("结 算")).isEnabled()) {
                    browser.findElement(By.linkText("结 算")).click();
                    // 下单
                    dealOrder(browser, endTime);
                    System.out.println("结算成功");
                    break;
                }

            }
        }
    }

    private static void dealOrder(ChromeDriver browser,Date endTime) {
        // https://buy.tmall.com/order/confirm_order.htm?spm=a1z0d.6639537.0.0.undefined
        // https://buy.tmall.com/order/confirm_order.htm?spm=a1z0d.6639537.0.0.undefined
        // https://buy.tmall.com/order/confirm_order.htm?spm=a1z0d.6639537.0.0.undefined
        browser.get("https://buy.tmall.com/order/confirm_order.htm?spm=a1z0d.6639537.0.0.undefined");
        for (int i = 0;i<30;i++) {
            try {
                browser.findElement(By.linkText("提交订单")).click();
                break;
            } catch (Exception e) {
                System.out.println("没有提交订单按钮");
            }
        }
    }
}
